<?php
namespace app\api\controller;

use app\common\model as M;

/**
 * 路线模块
 */
class Routes extends \app\api\ApiBase
{
    public $model = '\app\common\model\Routes';

    /**
     * 线路列表
     * @Author   FW
     * @DateTime 2024-03-13
     * @Email    123763712@qq.com
     * @return   [type]           [description]
     */
    public function routes()
    {
        return $this->returnSuccess($this->model::getList(input())['data']);
    }

    /**
     * 线路详情
     */ 
    public function details(int $route_id)
    {
        $detailsInfo = $this->model::where('id',$route_id)->with(['desposit','details'])->find();
        if ( empty($detailsInfo) ) return $this->returnFail('线路不存在');

        $detailsInfo->albums = json_decode($detailsInfo->albums);

        if ( !empty($detailsInfo->albums) ) {
            $albums = [];
            if ( $detailsInfo->main_video ) {
                $albums[] = videoToAlbum($detailsInfo->main_video);
            }
            foreach ( $detailsInfo->albums as $album ) {
                $albums[] = imageToAlbum($album);
            }
            $detailsInfo->albums = $albums;
        }
        $detailsInfo->label = json_decode($detailsInfo->label);
        ## 取4条SKU价格
        $detailsInfo->skuPrices = [];

        $currentLevelId = 0;
        if ( $this->uid > 0 ) {
            $currentLevelId = M\User::where('uid',$this->uid)->value('level_id');

            // 修改显示会员当前等级可享的会员价
            if ( $currentLevelId > 0 ) {
                $detailsInfo->prime_price = M\RoutesSkuPricePrime::where(['route_id'=>$route_id,'level_id'=>$currentLevelId])->where('level_price>0')->min('level_price');
            }
        }

        $skuPrices = M\RoutesSkuPrice::alias('t1')->field('t1.date,ifnull(t2.level_price,0) as price')
        ->join('__ROUTES_SKU_PRICE_PRIME__ t2','t1.route_id=t2.route_id AND t1.id=t2.skp_id AND t2.level_id='.$currentLevelId)
        ->where('t1.route_id',$route_id)
        ->where('t1.left_store>0')
        ->where('t1.date','>=',date('Y-m-d'))
        ->limit(4)
        ->order('t1.date ASC')
        ->select();

        if ( !empty($skuPrices) ) {
            $skuPrices = $skuPrices->toArray();
            foreach ( $skuPrices as &$skuPrice ) {
                $skuPrice['week'] = dateToWeek($skuPrice['date']);
                $skuPrice['date'] = date('m-d',strtotime($skuPrice['date']));
                $skuPrice['price']*=1;
            }
            $detailsInfo->skuPrices = $skuPrices;
        }

        ## 处理一下定金配置避免前端报错
        if ( empty($detailsInfo->desposit) ) $detailsInfo->desposit = ['deposit_amt' => 0];
        $detailsInfo->price *= 1;
        $detailsInfo->prime_price *= 1;
        return $this->returnSuccess($detailsInfo);

    }

    /**
     * 线路SKU列表
     */ 
    public function skuList(int $route_id)
    {
        $skuList = M\RoutesSku::field('id as sku_id,sku_name,allow_sold_monthes')->where('route_id',$route_id)->select();
        if ( empty($skuList) ) return $this->returnFail('数据不存在');

        $skuList = $skuList->toArray();
        foreach ( $skuList as &$skuItem ) {
            $monthes = json_decode($skuItem['allow_sold_monthes']);
            if ( empty($monthes) ) continue;

            $allowMonthFormat = [];
            foreach ( $monthes as $month ) {
                $allowMonthFormat[] = [
                    'name'      => date('m月',strtotime($month)),
                    'month'     => $month,
                    'month_num' => (int)date('m',strtotime($month))
                ];
            }
            $skuItem['allow_sold_monthes'] = $allowMonthFormat;
        }

        return $this->returnSuccess($skuList);
    }

    /**
     * 线路SKU PRICE
     */ 
    public function skuPrice(int $route_id, int $sku_id , $month)
    {
        $monthStart = date('Y-m-d' , strtotime('first day of '.$month));
        $monthEnd   = date('Y-m-d' , strtotime('last day of '.$month));
        $monthDays = [];

        $day = $monthStart;
        while ( strtotime($day) <= strtotime($monthEnd) ) {
            $monthDays[] = [
                'left_store' => 0,
                'week'       => (int)date('w',strtotime($day)),
                'price'      => 0,
                'date'       => $day,
                'date_text'  => date('n-j',strtotime($day)),
                'date_text2' => date('n月j日',strtotime($day)),
                'date_text3' => date('j',strtotime($day)),
                'expired'    => strtotime($day) < time() ? 1 : 0,
                'double_room'=> 0,
                'big_room'   => 0,
            ];
            $day = date('Y-m-d' , strtotime($day . ' +1 days'));
        }

        $currentLevelId = 0;
        if ( $this->uid > 0 ) {
            $currentLevelId = M\User::where('uid',$this->uid)->value('level_id');
        }

        $skuPrices = M\RoutesSkuPrice::alias('t1')->field('t1.date,t1.left_store,t1.double_room,t1.big_room,ifnull(t2.level_price,0) as price')
        ->join('__ROUTES_SKU_PRICE_PRIME__ t2','t1.route_id=t2.route_id AND t1.id=t2.skp_id AND t2.level_id='.$currentLevelId)
        ->where(['t1.route_id'=>$route_id,'t1.sku_id'=>$sku_id])
        ->where('date','BETWEEN',[$monthStart,$monthEnd])
        ->order('t1.date ASC')
        ->select();

        if ( !empty($skuPrices) ) {
            $skuPrices = $skuPrices->toArray();
            foreach ( $monthDays as &$monthDay ) {
                foreach ( $skuPrices as $skuKey => $skuPrice ) {
                    if ( $monthDay['date'] == $skuPrice['date'] ) {
                        $monthDay['left_store'] = $skuPrice['left_store'];
                        $monthDay['price']      = $skuPrice['price']*=1;
                        $monthDay['double_room']= $skuPrice['double_room'];
                        $monthDay['big_room']   = $skuPrice['big_room'];
                        unset($skuPrices[$skuKey]);
                        continue;
                    }
                }

            }
        }
        return $this->returnSuccess($monthDays);
    }
}//Class End